<?php
class BmembershipAction extends AdminPublicAction{
	public function index(){
		
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "uid";
			$_GET['updown'] = $upDown = "desc";
				
		}
		$map='';
	
		$keyword = I('keyword','');
		// 關鍵字查詢
		if(tep_not_null($keyword)){
			$where['name']  = array('like','%'.$keyword.'%');
			$where['member_id']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}

		$map['status']=1;
		import("ORG.Util.Page");
		$page = 10;
		$count = D('membership')->where($map)->count();
		
		$Page = new Page($count, $page);
		$infoList = D('MemberView')->getAll($map,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		//echo "<pre>";var_dump($infoList);die;
		
		if (S("success") != "") {
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "") {
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
		
		$this->keyword = $keyword;
		$this->page = $Page->show();
		$this->infoList = $infoList;
		$this->li1 = 'current';
		$this->left_Bmembership = 'current';
        $this->export_ext_url = U('ToExcel',array('keyword'=>$keyword));
		$this->display();
	}
	
/*======================================================================
    信息刪除
======================================================================================*/
    public function delete(){
        $id = I('uid');
        if(tep_not_null($id) && is_numeric($id)){	
            $result = M('membership')->where(array('uid'=>$id))->setField('status',0);
            if($result){
            	S('success','取消黑名单成功!');
            }else{
            	S('err','取消黑名单失败!');
            }
        }else{
            S('err','取消黑名单失败!');            
        }
        $this->redirect('index');
    }	

    // ajax 找到省份中的城市 
    public function findProCity() {
        $province = I('post.province');
        $where['parent_id'] = $province;
        $citys = D('City')->where($where)->select();
        $this->ajaxReturn($citys);
    }

 /*
     * 导出会员信息
     */
    public function ToExcel(){
        $name = I('keyword');

       if(tep_not_null($keyword)){
			$where['name']  = array('like','%'.$name.'%');
			$where['member_id']  = array('like','%'.$name.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}

		$map['status']=1;

       // $map = get_map_shequ_id($map,'shequid');

        $result = D('MemberView')->getMAll($map);

        // echo "<pre>";
         //var_dump($result);die;  
        
        $xls_name = "白名单会员列表";
        header("Content-type:application/vnd.ms-excel;charset=UTF-8");
        header("Content-Disposition:attachment;filename=$xls_name.xls"); 

        $table = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"	xmlns="http://www.w3.org/TR/REC-html40">
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html>
            <head>
            <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
            <style id="Classeur1_16681_Styles"></style>
            </head>';
        $table .= '<table style="border:1px solid #000; border-spacing:0px; width:1000px;text-align:left;border-collapse: collapse;">';
        $table .= '<thead><tr style="background:#c6c6c6;height:40px;"><th style="border:1px solid #000;">会员账号</th><th style="border:1px solid #000;">名称</th><th style="border:1px solid #000;">电话</th><th style="border:1px solid #000;">qq</th><th style="border:1px solid #000;">sina</th><th style="border:1px solid #000;">黑名单次数</th><th style="border:1px solid #000;">账户余额</th><th style="border:1px solid #000;">冻结金额</th><th style="border:1px solid #000;">提现总额</th><th style="border:1px solid #000;">国家</th><th style="border:1px solid #000;">省份</th><th style="border:1px solid #000;">城市</th></thead>';
        $table .= '<tbody>';
        foreach($result as $key=>$value){
            if(($key+1)%2==0){
                $table .= "<tr style='background:#a9c6c9'>";
            }else{
                $table .= "<tr>";
            }

            $table .= "<td style='border:1px solid #000;'>".htmlspecialchars($value['member_id'])."</td>";
            $table .= "<td style='border:1px solid #000;'>".$value['name'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['tel'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['qq'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['sina'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['black_count'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['account_money']['active_money'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['account_money']['freeze_money'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['withdraw'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['country'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['province'].'</td>';
            $table .= "<td style='border:1px solid #000;'>".$value['city'].'</td>';
            $table .= '</tr>';
        }
        $table .= '</tbody>';
        $table .= '</table>';
        echo $table;
    }
    
   
}
